package com.zt.ztlibrary.view;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.zt.ztlibrary.R;
import com.zt.ztlibrary.adapter.ZTAdapter;
import com.zt.ztlibrary.adapter.ZTInjector;
import com.zt.ztlibrary.adapter.model.LinkmanModel;
import com.zt.ztlibrary.adapter.viewinjector.IViewInjector;
import com.zt.ztlibrary.utils.ZTScrollListener;

import java.util.ArrayList;
import java.util.List;

public class StickyHeaderActivity extends AppCompatActivity {
    private static final String TAG = "StickyHeaderActivity";
    RecyclerView recyclerView;
    View headerView;
    List<Object> data = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sticky_header);
        recyclerView = findViewById(R.id.rl_sticky_header);
        headerView = findViewById(R.id.sticky_group_header);

        final ZTAdapter adapter = new ZTAdapter() {
            @Override
            public int getItemViewType(Object model, int position) {
                if (model instanceof String) {
                    return R.layout.layout_sticky_header;
                } else if (model instanceof LinkmanModel) {
                    return R.layout.layout_sticky_content;
                }
                return 0;
            }
        }
                .addHeader(this, R.layout.view_header)
                .addHeader(this, R.layout.view_header)
                .addHeader(this, R.layout.view_header)
                .addHeader(this, R.layout.view_header)
                .addFooter(this, R.layout.view_bottom)
                //列表分组头部
                .register(R.layout.layout_sticky_header, new ZTInjector<String>() {
                    @Override
                    public void onInject(String data, IViewInjector injector) {
                        Log.d(TAG, "当前加载下标" + injector.getPosition());
                        //组名字
                        injector.text(R.id.tv_sticky_group_name, data)
                                //头部设置 粘性 tag
                                .tag(R.id.sticky_group_header, ZTScrollListener.stickyView.EXIST_STICKY_VIEW);
                    }
                })
                //内容
                .register(R.layout.layout_sticky_content, new ZTInjector<LinkmanModel>() {
                    @Override
                    public void onInject(LinkmanModel data, IViewInjector injector) {
                        Log.d(TAG, "当前加载下标" + injector.getPosition());
                        //名字
                        injector.text(R.id.iv_sticky_name, data.getName());
                    }
                }).attachTo(recyclerView);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        //设置粘性头
        recyclerView.addOnScrollListener(new ZTScrollListener(headerView) {
            @Override
            public void onPositionHeaderView(View header, int position) {
                position -= adapter.getHeaderSize();
                if (position >= 0 && position < data.size()) {
                    TextView groupName = header.findViewById(R.id.tv_sticky_group_name);
                    Object model = data.get(position);
                    if (model instanceof String) {
                        groupName.setText((String) model);
                    } else if (model instanceof LinkmanModel) {
                        LinkmanModel linkmanModel = (LinkmanModel) model;
                        groupName.setText(linkmanModel.getGroupName());
                    }
                    header.setVisibility(View.VISIBLE);
                } else {
                    header.setVisibility(View.GONE);
                }

            }
        });

        for (int i = 1; i <= 10; i++) {
            String groupName = String.valueOf((char) (96 + i));
            data.add(groupName);
            for (int j = 0; j < 10; j++) {
                LinkmanModel linkmanModel = new LinkmanModel();
                linkmanModel.setGroupName(groupName);
                linkmanModel.setName(groupName + "  " + j + " 号");
                data.add(linkmanModel);
            }
        }
        adapter.updateData(data);
    }
}
